home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / SC++ 7.0.2 Update / TCL Demos / NewClassDemo ƒ / Demo Sources / CTableLabels.cp < prev   
Encoding:
Text File  |  1994-04-14  |  8.4 KB  |  341 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CTableLabels.h
  3.  
  4.         A subclass of CTable that is used to display row or column labels
  5.         for another CTable.
  6.         
  7.     SUPERCLASS = CTable
  8.     
  9.     Copyright © 1991 Symantec Corporation. All rights reserved.
  10.     
  11.  
  12.  ******************************************************************************/
  13.  
  14. #include "CTableLabels.h"
  15. #include "CTableScroller.h"
  16. #include "CTable.h"
  17. #include "CRowColSizer.h"
  18. #include "CRunArray.h"
  19. #include "CPaneBorder.h"
  20. #include "Packages.h"
  21.  
  22. #define kGrowColCursor        1025    // resize column cursor ID
  23. #define kGrowRowCursor        1026    // resize row column cursor ID
  24.  
  25. #define kSlopAmount            3        // slop amount in pixels for recognizing
  26.                                     // row or column border clicks
  27.  
  28. /******************************************************************************
  29.  ITableLabels
  30.  
  31.      Initialization method for CTableLabels. aTblScroller is a subclass of
  32.      CScrollPane that aids in synchronizing the scrolling of the table and
  33.      labels. rowsOrCols indicates what type of labels this table is to
  34.      display. size is the desired height of row labels or desired width of
  35.      column labels. anOffset is a horizontal or vertical offset needed
  36.      to line the labels up with the table.
  37. ******************************************************************************/
  38.  
  39. void CTableLabels::ITableLabels(CTableScroller *aTblScroller, VHSelect rowsOrCols, 
  40.                     short size, short anOffset)
  41. {
  42.     Rect delta;
  43.     
  44.     itsTable = NULL;
  45.  
  46.     whichDimension = rowsOrCols;
  47.     
  48.     SetRect(&delta, 0, 0, 0, 0);
  49.     
  50.     if (whichDimension == h)    /* column labels    */
  51.     {
  52.         CTable::ITable(aTblScroller, aTblScroller, 0, size, 0, 0,
  53.                     sizELASTIC, sizFIXEDTOP);
  54.         FitToEnclosure(kDoHorizontal, kNotVertical);
  55.         Offset(anOffset, 0, kNoRedraw);
  56.         delta.right = -anOffset;
  57.     }
  58.     else                        /* row labels        */
  59.     {
  60.         CTable::ITable(aTblScroller, aTblScroller, size, 0, 0, 0,
  61.                     sizFIXEDLEFT, sizELASTIC);
  62.         FitToEnclosure(kNotHorizontal, kDoVertical);
  63.         Offset(0, anOffset, kNoRedraw);
  64.         delta.bottom = -anOffset;
  65.     }
  66.     ChangeSize(&delta, kNoRedraw);
  67.     
  68.     ITableLabelsX();
  69. }
  70.  
  71. /******************************************************************************
  72.  ITableLabelsX
  73.  
  74.      Extra initialization for CTableLabels. Fills in additional instance
  75.      variables, and border.
  76. ******************************************************************************/
  77.  
  78. void CTableLabels::ITableLabelsX(void)
  79. {
  80.     short    cursID;
  81.     short    borderFlags;
  82.     
  83.     SetCanBeGopher(FALSE);
  84.     SetSelectionFlags(selDragRects);
  85.     textStyle = bold;
  86.     initiatedNewSelection = FALSE;    
  87.  
  88.     if (whichDimension == h)
  89.     {
  90.         cursID = kGrowColCursor;
  91.         drawOrder = tblRow;
  92.         AddRow(1, -1);
  93.         SetColBorders(1, patCopy, &qd.black);
  94.         borderFlags = kBorderBottom;
  95.     }
  96.     else
  97.     {
  98.         cursID = kGrowRowCursor;
  99.         drawOrder = tblCol;    
  100.         AddCol(1, -1);
  101.         SetRowBorders(1, patCopy, &qd.black);
  102.         borderFlags = kBorderRight;
  103.     }
  104.  
  105.     itsBorder = new(CPaneBorder);
  106.     itsBorder->IPaneBorder(borderFlags);
  107.  
  108.     growCursor = GetCursor(cursID);
  109.     FailNILRes(growCursor);
  110.     
  111. }
  112.  
  113. /******************************************************************************
  114.  ~CTableLabels
  115.  
  116.      Since CTableLabels shares either the row or column size array with
  117.      its table, we must ensure that the shared array is not disposed when
  118.      the table labels are disposed.
  119. ******************************************************************************/
  120.  
  121. CTableLabels::~CTableLabels(void)
  122. {
  123.     /* don't dispose of objects owned by itsTable */
  124.     if (itsTable)
  125.     {
  126.         if (whichDimension == h)
  127.             itsCols = NULL;
  128.         else
  129.             itsRows = NULL;
  130.     }    
  131. }
  132.  
  133. /******************************************************************************
  134.  SetTable
  135.  
  136.      Specify the table for which this object is to provide labels. CTableLabels
  137.      shares the row or column size array with itsTable. This ensures that
  138.      both tables have the same number of rows or columns with the same dimensions.
  139. ******************************************************************************/
  140.  
  141. void CTableLabels::SetTable(CTable *aTable)
  142. {
  143.     itsTable = aTable;
  144.     if (whichDimension == h)
  145.     {
  146.         ForgetObject(itsCols);
  147.         itsCols = ((CTableLabels*)itsTable)->itsCols;
  148.         DependUpon(itsCols);
  149.         SetRowHeight(0, height);
  150.     }
  151.     else
  152.     {
  153.         ForgetObject(itsRows);
  154.         itsRows = ((CTableLabels*)itsTable)->itsRows;
  155.         DependUpon(itsRows);
  156.         SetColWidth(0, width);
  157.     }
  158.     DependUpon(itsTable);
  159. }
  160.  
  161. /******************************************************************************
  162.  Draw {OVERRIDE}
  163.  
  164.      This method does some setup before drawing the labels. It restores the
  165.      drawing environment of itsTable, to ensure the both draw with the
  166.      same environment.
  167. ******************************************************************************/
  168.  
  169. void CTableLabels::Draw(Rect *area)
  170. {
  171.     ASSERT(itsTable != NULL);
  172.  
  173.     itsTable->RestoreEnvironment();
  174.     TextFace(textStyle);
  175.     
  176.     tblIndent = ((CTableLabels*)itsTable)->indent;
  177.     
  178.     inherited::Draw(area);
  179. }
  180.  
  181. /******************************************************************************
  182.  DrawCell {OVERRIDE}
  183.  
  184.      Draw the row or column number.
  185. ******************************************************************************/
  186.  
  187. void CTableLabels::DrawCell(Cell theCell, Rect *cellRect)
  188. {
  189.     Str31    numBuf;
  190.     short    val;
  191.     short    sWidth;
  192.     
  193.     
  194.     val = (whichDimension == h)? theCell.h : theCell.v;
  195.     val++;
  196.     
  197.     NumToString(val, numBuf);
  198.     sWidth = StringWidth(numBuf);
  199.     
  200.     MoveTo(cellRect->left + (cellRect->right - cellRect->left - sWidth)/2,
  201.             cellRect->top + tblIndent.v);
  202.     
  203.     DrawString(numBuf);    
  204.  
  205. }
  206.  
  207. /******************************************************************************
  208.  GetHitBoundary
  209.  
  210.      Determine whether a point in Frame coordinates lies on a row or column
  211.      boundary. This method is used both for hit testing clicks and for 
  212.      adjusting the cursor.
  213.      
  214. ******************************************************************************/
  215.  
  216. short CTableLabels::GetHitBoundary(LongPt *hitPt)
  217. {
  218.     short boundary = -1, rowOrCol;
  219.     long  start, mouseLoc, diff;
  220.     
  221.     if (whichDimension == h)
  222.     {
  223.         mouseLoc = hitPt->h + kSlopAmount;
  224.         rowOrCol = FindCol(mouseLoc);
  225.         if ((rowOrCol >= 0) && (rowOrCol < tableBounds.right))
  226.         {
  227.             start = GetColStart(rowOrCol);
  228.         }
  229.         else rowOrCol = -1;
  230.     }
  231.     else
  232.     {
  233.         mouseLoc = hitPt->v + kSlopAmount;
  234.         rowOrCol = FindRow(mouseLoc);
  235.         if ((rowOrCol > 0) && (rowOrCol < tableBounds.bottom))
  236.         {
  237.             start = GetRowStart(rowOrCol);
  238.         }
  239.         else rowOrCol = -1;
  240.     }
  241.     diff = mouseLoc - start - kSlopAmount;
  242.     if (Abs(diff) <= kSlopAmount)
  243.         boundary = rowOrCol-1;
  244.         
  245.     return boundary;
  246. }
  247.  
  248. /******************************************************************************
  249.  DoClick
  250. ******************************************************************************/
  251.  
  252. void CTableLabels::DoClick(Point hitPt, short modifierKeys, long when)
  253. {
  254.     short     boundary;
  255.     LongPt    framePt;
  256.     CRowColSizer    *sizeTask;
  257.     LongRect    limitRect;
  258.     
  259.     QDToFrame(hitPt, &framePt);
  260.     boundary = GetHitBoundary(&framePt);
  261.     if (boundary >= 0)
  262.     {
  263.         itsTable->GetInterior(&limitRect);
  264.         sizeTask = new CRowColSizer(itsTable, this, boundary, whichDimension,
  265.             &limitRect, 0);
  266.         
  267.         itsLastTask = sizeTask;
  268.         itsTable->itsLastTask = sizeTask;
  269.         
  270.         FrameToWind(&framePt, &hitPt);
  271.         itsTable->WindToFrame(hitPt, &framePt);
  272.         
  273.         itsTable->TrackMouse(sizeTask, &framePt, &limitRect);
  274.         
  275.         Notify(sizeTask);
  276.         Refresh();
  277.     }
  278. }
  279.  
  280. /******************************************************************************
  281.  AdjustCursor
  282. ******************************************************************************/
  283.  
  284. void CTableLabels::AdjustCursor(Point where, RgnHandle mouseRgn)
  285. {
  286.     short     boundary;
  287.     LongPt    framePt;
  288.     extern long gSleepTime;
  289.     
  290.     Prepare();
  291.     WindToFrame(where, &framePt);
  292.     boundary = GetHitBoundary(&framePt);
  293.     if (boundary >= 0)
  294.     {
  295.         LoadResource((Handle) growCursor);
  296.         if (*growCursor)
  297.             SetCursor(*growCursor);
  298.     }
  299.     else
  300.         SetCursor(&qd.arrow);
  301.     gSleepTime = 0;
  302. }
  303.  
  304. /******************************************************************************
  305.  ProviderChanged
  306.  
  307. ******************************************************************************/
  308.  
  309. void CTableLabels::ProviderChanged(CCollaborator *aProvider, 
  310.                     long reason, void* info)
  311. {
  312.     short    newSize = *(long*) info;
  313.     Boolean changed = FALSE;
  314.     
  315.     if (reason == runArraySizeChanged)
  316.     {        
  317.         if ((whichDimension == h) && (aProvider == itsCols))
  318.         {
  319.             tableBounds.right = newSize;
  320.             changed = TRUE;
  321.         }
  322.         else if (aProvider == itsRows)
  323.         {
  324.             tableBounds.bottom = newSize;
  325.             changed = TRUE;
  326.         }
  327.         if (changed)
  328.         {
  329.             AdjustBounds();
  330.             Refresh();
  331.         }
  332.     }
  333.     else if (reason == runArrayElementChanged)        // TCL 1.1.3 11/18/92
  334.     {
  335.         AdjustBounds();
  336.         Refresh();
  337.     }
  338.     else
  339.         inherited::ProviderChanged(aProvider, reason, info);
  340. }
  341.